package com.mall.order.services;

import com.mall.order.StockAddCartService;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.StockAddCartResponse;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * create by heyyon 2020-12-21 15:02
 */
@Slf4j
@Component
@Service
public class StockAddCartServiceImpl implements StockAddCartService {
    @Autowired
    StockMapper stockMapper;

    @Override
    public StockAddCartResponse queryGoodsStock(Long productId) {
        StockAddCartResponse stockAddCartResponse = new StockAddCartResponse();
        try {
            Integer goodsStock = stockMapper.selectGoodsStock(productId);
            if (goodsStock==null||goodsStock == 0) {
                stockAddCartResponse.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
                stockAddCartResponse.setMsg("没有该商品暂时无货，换一个试试吧");
                return stockAddCartResponse;
            }
            stockAddCartResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            stockAddCartResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            stockAddCartResponse.setNum(goodsStock);
        } catch (Exception e) {
            log.error("StockAddCartServiceImpl.queryGoodsStock Occur Exception:"+e);
            ExceptionProcessorUtils.wrapperHandlerException(stockAddCartResponse,e);
        }
        return stockAddCartResponse;
    }
    //添加至购物车时，更新库存数量，暂不需要了
    @Override
    public StockAddCartResponse updateGoodsStock(Long productId, Long productNum) {
        StockAddCartResponse stockAddCartResponse = new StockAddCartResponse();
        try {
            int row=stockMapper.updateGoodsStock(productId, productNum);
            if (row == 0) {
                stockAddCartResponse.setCode(SysRetCodeConstants.DB_EXCEPTION.getCode());
                stockAddCartResponse.setMsg(SysRetCodeConstants.DB_EXCEPTION.getMessage());
            }
            stockAddCartResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            stockAddCartResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("StockAddCartServiceImpl.queryGoodsStock Occur Exception:"+e);
            ExceptionProcessorUtils.wrapperHandlerException(stockAddCartResponse,e);
        }
        return stockAddCartResponse;
    }
}
